package com.morehair.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.morehair.bean.bo.admin.CategoryBo;
import com.morehair.bean.bo.admin.RefundBo;
import com.morehair.bean.bo.admin.ShipBo;
import com.morehair.bean.pojo.*;
import com.morehair.bean.vo.admin.*;
import com.morehair.mapper.*;
import com.morehair.service.admin.ShopManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author raohui
 * @Date 2021/6/5 7:55
 */
@Service
@Transactional
public class ShopManageServiceImpl implements ShopManageService {

    @Autowired
    RegionMapper regionMapper;

    @Autowired
    GoodsMapper goodsMapper;

    ArrayList<RegionDataVo> regionDataVos = new ArrayList<>();

    @Override
    public List<RegionDataVo> selectRegion() {
        // if (regionDataVos.size() == 0) {
        RegionExample regionExample = new RegionExample();
        //RegionExample.Criteria criteria = regionExample.createCriteria();
        //查询region所有数据
        List<Region> regions = regionMapper.selectByExample(regionExample);
        for (Region region : regions) {
            if (region.getType().equals(((byte) 1))) {
                RegionDataVo regionDataVo = new RegionDataVo();
                regionDataVo.setId(region.getId());
                regionDataVo.setName(region.getName());
                regionDataVo.setType(region.getType());
                regionDataVo.setCode(region.getCode());
                regionDataVo.setChildren(new ArrayList<RegionDataVo.ChildrenBeanX>());
                regionDataVos.add(regionDataVo);
            }
            if (region.getType().equals(((byte) 2))) {
                RegionDataVo.ChildrenBeanX childrenBeanX = new RegionDataVo.ChildrenBeanX();
                childrenBeanX.setId(region.getId());
                childrenBeanX.setName(region.getName());
                childrenBeanX.setCode(region.getCode());
                childrenBeanX.setChildren(new ArrayList<>());
                String code = region.getCode().toString().substring(0, 2);
                for (RegionDataVo regionDataVo : regionDataVos) {
                    if (Integer.toString(regionDataVo.getCode()).equals(code)) {
                        regionDataVo.getChildren().add(childrenBeanX);
                    }
                }
            }
            if (region.getType().equals(((byte) 3))) {
                RegionDataVo.ChildrenBeanX.ChildrenBean childrenBean = new RegionDataVo.ChildrenBeanX.ChildrenBean();
                childrenBean.setId(region.getId());
                childrenBean.setName(region.getName());
                childrenBean.setType(region.getType());
                childrenBean.setCode(region.getCode());
                String codeChild = region.getCode().toString().substring(0, 4);
                for (RegionDataVo regionDataVo : regionDataVos) {
                    for (RegionDataVo.ChildrenBeanX child : regionDataVo.getChildren()) {
                        if (Integer.toString(child.getCode()).equals(codeChild)) {
                            child.getChildren().add(childrenBean);
                        }
                    }
                }
            }
        }
        //}
        return regionDataVos;
    }

    @Autowired
    BrandMapper brandMapper;

    @Override
    public BrandDataVo selectBrandList(Integer page, Integer limit, Integer id, String name, String sort, String order) {
        //分页插件,自动执行sql语句
        PageHelper.startPage(page, limit);
        BrandExample brandExample = new BrandExample();
        brandExample.setOrderByClause(sort + " " + order);
        BrandExample.Criteria criteria = brandExample.createCriteria();
        //条件判断
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);
        //查询
        List<Brand> brands = brandMapper.selectByExample(brandExample);

        //获取分页信息
        PageInfo<Brand> brandPageInfo = new PageInfo<>(brands);
        long total = brandPageInfo.getTotal();
        BrandDataVo brandDataVo = new BrandDataVo();
        brandDataVo.setTotal((int) total);
        brandDataVo.setItems(brands);
        return brandDataVo;
    }


    @Override
    public int deleteBrand(Brand brand) {
        brand.setUpdateTime(new Date());
        brand.setDeleted(true);
        int i = brandMapper.updateByPrimaryKey(brand);
        if (i != 0) {
            return i;
        }
        return 0;
    }

    @Override
    public void updateBrand(Brand brand) {
        brand.setUpdateTime(new Date());
        brandMapper.updateByPrimaryKey(brand);
    }

    @Override
    public void createBrand(Brand brand) {
        Brand brandData = new Brand(null,brand.getName(),brand.getDesc(),brand.getPicUrl(),null,brand.getFloorPrice(),new Date(),new Date(),false);
        brandMapper.insertSelective(brandData);
    }


    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<CategoryDataVo> selectCategoryList() {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        //查询表Catagory中delete为false的所有数据
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        ArrayList<CategoryDataVo> categoryDataVos = new ArrayList<>();
        for (Category category : categories) {
            if (category.getLevel().equals("L1")) {
                CategoryDataVo categoryDataVo = new CategoryDataVo();
                categoryDataVo.setId(category.getId());
                categoryDataVo.setDesc(category.getDesc());
                categoryDataVo.setIconUrl(category.getIconUrl());
                categoryDataVo.setKeywords(category.getKeywords());
                categoryDataVo.setLevel(category.getLevel());
                categoryDataVo.setName(category.getName());
                categoryDataVo.setPicUrl(category.getPicUrl());
                categoryDataVo.setChildren(new ArrayList<CategoryDataVo.ChildrenBean>());
                categoryDataVos.add(categoryDataVo);
            }
        }
            for (Category category : categories) {
                if (category.getLevel().equals("L2")) {
                    CategoryDataVo.ChildrenBean childrenBean = new CategoryDataVo.ChildrenBean();
                    childrenBean.setDesc(category.getDesc());
                    childrenBean.setIconUrl(category.getIconUrl());
                    childrenBean.setId(category.getId());
                    childrenBean.setKeywords(category.getKeywords());
                    childrenBean.setLevel(category.getLevel());
                    childrenBean.setName(category.getName());
                    childrenBean.setPicUrl(category.getPicUrl());
                    for (CategoryDataVo categoryDataVo : categoryDataVos) {
                        if (category.getPid().equals(categoryDataVo.getId())) {
                            categoryDataVo.getChildren().add(childrenBean);
                        }
                    }
                }
            }
        return categoryDataVos;
    }

    @Override
    public List<CategoryDataVo2> selectCategoryL1() {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        //查询表Catagory所有数据
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        List<CategoryDataVo2> categoryDataVo2s = new ArrayList<>();
        for (Category category : categories) {
            if (category.getLevel().equals("L1")) {
                CategoryDataVo2 categoryDataVo2 = new CategoryDataVo2();
                categoryDataVo2.setLabel(category.getName());
                categoryDataVo2.setValue(category.getId());
                categoryDataVo2s.add( categoryDataVo2);
            }
        }
        return categoryDataVo2s;
    }

    @Override
    public void updateCategory(Category category) {
        category.setDeleted(false);
        if (category.getPid()== null) {
            category.setPid(0);
            categoryMapper.updateByPrimaryKey(category);
        }
        categoryMapper.updateByPrimaryKey(category);
    }

    @Override
    public void createCategory(Category category) {
        Category categoryData = new Category(null,category.getName(),category.getKeywords(),category.getDesc(),category.getPid(),category.getIconUrl(),category.getPicUrl(),category.getLevel(), ((byte) 1),new Date(),new Date(),false);
        categoryMapper.insertSelective(categoryData);

    }


    @Override
    public void deleteLevelLCategory(CategoryBo categoryBo) {
        Category category = new Category(categoryBo.getId(),categoryBo.getName(),categoryBo.getKeywords(),categoryBo.getDesc(),null,categoryBo.getIconUrl(),categoryBo.getPicUrl(),categoryBo.getLevel(),null,null,new Date(),true);
        category.setDeleted(true);//假删除,0代表false(会在前端显示)，1代表true（不会在前端显示）
        categoryMapper.updateByPrimaryKeySelective(category);
        //删除子目录
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andPidEqualTo(category.getId());
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        for (Category deleteSubCategory : categories) {
            deleteSubCategory.setDeleted(true);
            deleteSubCategory.setUpdateTime(new Date());
            categoryMapper.updateByPrimaryKeySelective(deleteSubCategory);
        }
    }




    @Autowired
    OrderMapper orderMapper;

    @Override
    public OrderDataVo selectOrderList(Integer page, Integer limit, Integer orderStatusArray, String sort, String order, String orderSn, Integer userId) {
        //分页插件，自动生成sql语句
        PageHelper.startPage(page, limit);
        OrderExample orderExample = new OrderExample();
        //排序信息
        orderExample.setOrderByClause(sort + " " + order);
        OrderExample.Criteria criteria = orderExample.createCriteria();
        if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if (orderSn !=null && !"".equals(orderSn)) {
            criteria.andOrderSnLike("%"+orderSn+"%");
        }
        if (orderStatusArray !=null) {
            criteria.andOrderStatusEqualTo(orderStatusArray);
        }
        criteria.andDeletedEqualTo(false);
        //查询orderb表中所有数据
        List<Order> orders = orderMapper.selectByExample(orderExample);
        //获取分页信息
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        long total = orderPageInfo.getTotal();

        OrderDataVo orderDataVo = new OrderDataVo();
        orderDataVo.setTotal((int) total);
        orderDataVo.setItems(orders);
        return orderDataVo;
    }


    @Autowired
    OrderGoodsMapper orderGoodsMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public OrderDetailDataVo selectOrderDetail(Integer id) {
        //order表
        Order order = orderMapper.selectByPrimaryKey(id);
        OrderDetailByOrderVo orderDetailByOrderVo = new OrderDetailByOrderVo();
        orderDetailByOrderVo.setId(order.getId());
        orderDetailByOrderVo.setUserId(order.getUserId());
        orderDetailByOrderVo.setOrderSn(order.getOrderSn());
        orderDetailByOrderVo.setOrderStatus(order.getOrderStatus());
        orderDetailByOrderVo.setConsignee(order.getConsignee());
        orderDetailByOrderVo.setMobile(order.getMobile());
        orderDetailByOrderVo.setAddress(order.getAddress());
        orderDetailByOrderVo.setMessage(order.getMessage());
        orderDetailByOrderVo.setGoodsPrice(order.getGoodsPrice().intValue());
        orderDetailByOrderVo.setFreightPrice(order.getFreightPrice().intValue());
        orderDetailByOrderVo.setCouponPrice(order.getCouponPrice().intValue());
        orderDetailByOrderVo.setOrderPrice(order.getOrderPrice().intValue());
        orderDetailByOrderVo.setActualPrice(order.getActualPrice().intValue());
        orderDetailByOrderVo.setComments(order.getComments());
        orderDetailByOrderVo.setAddTime(order.getAddTime());
        orderDetailByOrderVo.setUpdateTime(order.getUpdateTime());
        orderDetailByOrderVo.setEndTime(order.getEndTime());
        orderDetailByOrderVo.setDeleted(false);
        //orderGoods表
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(order.getUserId());
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
        //user表
        OrderDetailByUserVo orderDetailByUserVo=userMapper.selectUserById(order.getUserId());
        OrderDetailDataVo orderDetailDataVo = new OrderDetailDataVo();
        orderDetailDataVo.setOrder(orderDetailByOrderVo);
        orderDetailDataVo.setUser(orderDetailByUserVo);
        orderDetailDataVo.setOrderGoods(orderGoods);
        return orderDetailDataVo;
    }

    @Override
    public void refundOrder(RefundBo refundBo) {
        Order order = new Order();
        order.setId(refundBo.getOrderId());
        order.setOrderStatus((short) 203);
        order.setUpdateTime(new Date());
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public void shipOrder(ShipBo shipBo) {
        Order order = new Order();
        order.setId(shipBo.getOrderId());
        order.setShipChannel(shipBo.getShipChannel());
        order.setShipSn(shipBo.getShipSn());
        order.setOrderStatus((short) 301);
        order.setShipTime(new Date());
        order.setUpdateTime(new Date());
        orderMapper.updateByPrimaryKeySelective(order);
    }



    @Autowired
    IssueMapper issueMapper;

    @Override
    public IssueDataVo selectIssueList(Integer page, Integer limit, String question, String sort, String order) {
        //分页插件，自动生成sql语句
        PageHelper.startPage(page, limit);
        IssueExample issueExample = new IssueExample();
        //排序信息
        issueExample.setOrderByClause(sort + " " + order);
        IssueExample.Criteria criteria = issueExample.createCriteria();
        if (question !=null && !"".equals(question)) {
            criteria.andQuestionLike("%"+question+"%");
        }
        criteria.andDeletedEqualTo(false);
        //查询orderb表中所有数据
        List<Issue> issues = issueMapper.selectByExample(issueExample);
        //获取分页信息
        PageInfo<Issue> issuePageInfo = new PageInfo<>(issues);
        long total = issuePageInfo.getTotal();

        IssueDataVo issueDataVo = new IssueDataVo();
        issueDataVo.setTotal((int) total);
        issueDataVo.setItems(issues);
        return issueDataVo ;
    }

    @Override
    public void deleteIssue(Issue issue) {
        issue.setDeleted(true);
        issueMapper.updateByPrimaryKey(issue);
    }

    @Override
    public Issue updateIssue(Issue issue) {
        issue.setUpdateTime(new Date());
        issueMapper.updateByPrimaryKey(issue);
        Issue issue1=issueMapper.selectUpdateIssue(issue.getId());
        return issue1;
    }

    @Override
    public Issue createIssue(Issue issue) {
        issueMapper.insertSelective(issue);
        Issue issue1=issueMapper.selectCreateIssue();
        return issue1;
    }



    @Autowired
    KeyWordMapper keyWordMapper;
    @Override
    public KeyWordDataVo selectKeywordList(Integer page, Integer limit, String url, String keyword, String sort, String order) {
        //分页插件，自动生成sql语句
        PageHelper.startPage(page, limit);
        KeyWordExample keyWordExample = new KeyWordExample();
        //排序信息
        keyWordExample.setOrderByClause(sort + " " + order);
        KeyWordExample.Criteria criteria = keyWordExample.createCriteria();
        if (url !=null && !"".equals(url)) {
            criteria.andUrlLike("%"+url+"%");
        }
        if (keyword !=null && !"".equals(keyword)) {
            criteria.andKeywordLike("%"+keyword+"%");
        }
        criteria.andDeletedEqualTo(false);
        //查询orderb表中所有数据
        List<KeyWord> keyWords = keyWordMapper.selectByExample(keyWordExample);
        //获取分页信息
        PageInfo<KeyWord> keyWordPageInfo = new PageInfo<>(keyWords);
        long total = keyWordPageInfo.getTotal();

        KeyWordDataVo keyWordDataVo = new KeyWordDataVo();
        keyWordDataVo.setTotal((int) total);
        keyWordDataVo.setItems(keyWords);
        return keyWordDataVo ;
    }

    @Override
    public void deleteKeyWord(KeyWord keyWord) {
        keyWord.setDeleted(true);
        keyWord.setUpdateTime(new Date());
        keyWordMapper.updateByPrimaryKeySelective(keyWord);
    }

    @Override
    public KeyWord updateKeyWord(KeyWord keyWord) {
        keyWordMapper.updateByPrimaryKeySelective(keyWord);
        KeyWord keyWord1=keyWordMapper.selectKeyWord(keyWord.getId());
        return keyWord1;
    }

    @Override
    public KeyWord createKeyWord(KeyWord keyWord) {
        keyWordMapper.insertSelective(keyWord);
        KeyWord keyWord1=keyWordMapper.selectCreateKeyWord();
        return keyWord1;
    }

}

